package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.order.client.CartManageClient;
import com.woniu.order.client.param.AddCartParam;
import com.woniu.order.client.param.ModifyCartParam;
import com.woniu.order.service.CartService;
import com.woniu.order.service.param.AddCartServiceParam;
import com.woniu.order.service.param.DelCartServiceParam;
import com.woniu.order.service.param.ModifyCartServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class CartManageApiImpl implements CartManageClient {

    @Resource
    private CartService cartService;

    /**
     * 加入购物车
     *
     * @param addCartParam
     * @return
     */
    @Override
    public Result addCart(AddCartParam addCartParam) {
        log.info("加入购物车：{}", addCartParam);
        //获取userId
        Integer userId = 1;
        AddCartServiceParam addCartServiceParam = BeanUtil.copyProperties(addCartParam, AddCartServiceParam.class);
        addCartServiceParam.setUserId(userId);
        cartService.addCart(addCartServiceParam);
        return Result.getInstance();
    }

    /**
     * 删除购物车某条商品
     */
    @Override
    public Result delCart(Integer skuId) {
        log.info("删除购物车某条商品：{}", skuId);
        //获取userId
        Integer userId = 1;
        DelCartServiceParam delCartServiceParam = new DelCartServiceParam();
        delCartServiceParam.setUserId(userId);
        delCartServiceParam.setSkuId(skuId);
        cartService.delCart(delCartServiceParam);
        return Result.getInstance();
    }

    /**
     * 修改购物车商品数量
     * @param modifyCartParam
     * @return
     */
    @Override
    public Result modifyCart(ModifyCartParam modifyCartParam) {
        log.info("修改购物车商品数量：{}", modifyCartParam);
        //获取userId
        Integer userId = 1;
        ModifyCartServiceParam modifyCartServiceParam = BeanUtil.copyProperties(modifyCartParam, ModifyCartServiceParam.class);
        modifyCartServiceParam.setUserId(userId);
        cartService.modifyCart(modifyCartServiceParam);
        return Result.getInstance();
    }
}
